package com.glut.gultguy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.glut.gultguy.To.GoodsAuctionRecordTo;
import com.glut.gultguy.entity.Goods;
import com.glut.gultguy.entity.GoodsAuctionRecord;
import com.glut.gultguy.entity.User;
import com.glut.gultguy.mapper.GoodsAuctionRecordMapper;
import com.glut.gultguy.mapper.GoodsMapper;
import com.glut.gultguy.mapper.UserMapper;
import com.glut.gultguy.service.GoodsAuctionRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glut.gultguy.utils.DateUtils;
import com.glut.gultguy.utils.SnowflakeIdWorker;
import com.glut.gultguy.vo.GoodsAuctionRecordVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-09-05
 */
@Service
public class GoodsAuctionRecordServiceImpl extends ServiceImpl<GoodsAuctionRecordMapper, GoodsAuctionRecord> implements GoodsAuctionRecordService {

    @Autowired(required = false)
    private SnowflakeIdWorker idWorker;

    @Autowired(required = false)
    private DateUtils dateUtils;

    @Autowired(required = false)
    private GoodsAuctionRecordMapper mapper;

    @Autowired(required = false)
    private UserMapper userMapper;


    @Autowired(required = false)
    private GoodsMapper goodsMapper;
    @Override
    public Map<String,Integer> setGoodsRecord(GoodsAuctionRecordTo to) {
        GoodsAuctionRecord record = new GoodsAuctionRecord();
        Map<String,Integer> map = new HashMap<>();
        Goods goods = goodsMapper.selectOne(new QueryWrapper<Goods>().eq("id",to.getGoodsID()));
        if (goods.getGoodsState()==3){
            map.put("code",500);
            return map;
        }
        record.setGoodsId(to.getGoodsID());
        record.setAuctionTime(new Date());
        record.set_id(idWorker.nextId()+"");
        record.set_openid(to.getUserInfo().get_openid());
        record.setPutPrice(to.getPutPrice());
        int t = mapper.insert(record);
        if (t==1){
            map.put("code",200);
        }else {
            map.put("code",500);
        }
        return map;
    }

    @Override
    public List<GoodsAuctionRecordVo> getRecordList(String goodsID) {
        String id = goodsID;
        List<GoodsAuctionRecordVo> vos =   mapper.selectList(new QueryWrapper<GoodsAuctionRecord>().eq("goodsId",id)).stream()
               .map(goodsAuctionRecord -> {
                   GoodsAuctionRecordVo vo = new GoodsAuctionRecordVo();
                   BeanUtils.copyProperties(goodsAuctionRecord,vo);
                   vo.setGoodsID(id);
                   vo.setAuctionTimeFormat(goodsAuctionRecord.getAuctionTime());
                   vo.setAuctionTime(dateUtils.getTimeStamp(goodsAuctionRecord.getAuctionTime(),1));
                 User user = userMapper.selectOne(new QueryWrapper<User>().eq("_openid",goodsAuctionRecord.get_openid()));
                  vo.setUserInfo(user);

                   return vo;
               }).collect(Collectors.toList());
        return vos;
    }
}
